package k.daniel.android.view;

import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;

import java.lang.ref.WeakReference;

/**
 * Created by daniel on 2015/4/27.
 */
public class ImageHandler extends Handler implements ViewPager.OnPageChangeListener {
    enum State {
        SLEEP, RUN, STOP
    }

    State handlerState = State.STOP;
    /**
     * 请求更新显示的View。
     */
    protected static final int MSG_UPDATE_IMAGE = 1;
    //轮播间隔时间
    protected static final long MSG_DELAY = 3000;
    //使用弱引用避免Handler泄露.这里的泛型参数可以不是Activity，也可以是Fragment等
    private WeakReference<ViewPager> weakReference;
    private int currentItem = 0;

    public ImageHandler(ViewPager viewPager) {
        weakReference = new WeakReference<ViewPager>(viewPager);
    }
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        ViewPager viewPager = weakReference.get();
        if (viewPager == null) {
            //Activity已经回收，无需再处理UI了
            return;
        }
        //检查消息队列并移除未发送的消息，这主要是避免在复杂环境下消息出现重复等问题。
        if (hasMessages(MSG_UPDATE_IMAGE)) {
            removeMessages(MSG_UPDATE_IMAGE);
        }
        if(handlerState!=State.RUN){
            System.out.println("状态异常.");
            return;
        }
        switch (msg.what) {
            case MSG_UPDATE_IMAGE:
                currentItem++;
                if(currentItem>=viewPager.getAdapter().getCount()&&viewPager.getAdapter().getCount()>0)
                {
                    currentItem=currentItem%viewPager.getAdapter().getCount();
                }
                viewPager.setCurrentItem(currentItem);
                //准备下次播放
                sendEmptyMessageDelayed(MSG_UPDATE_IMAGE, MSG_DELAY);
                break;
            default:
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        switch (state) {
            case ViewPager.SCROLL_STATE_DRAGGING:
                //手动滑动
                sleep();
                break;
            case ViewPager.SCROLL_STATE_SETTLING:
                if (handlerState == State.SLEEP) {
                    //手动滑动结束后会触发
                    //当当前的状态发生变化
                    currentItem=weakReference.get().getCurrentItem();
                    resume();
//                    sendEmptyMessageDelayed(ImageHandler.MSG_UPDATE_IMAGE, ImageHandler.MSG_DELAY);
                }
                break;
            default:
                break;
        }
    }

    private void resume() {
        startLoop();
    }

    private void sleep() {
        removeMessages(MSG_UPDATE_IMAGE);
        handlerState = State.SLEEP;
    }

    public void startLoop() {
        handlerState = State.RUN;
        removeMessages(MSG_UPDATE_IMAGE);
        sendEmptyMessageDelayed(MSG_UPDATE_IMAGE, MSG_DELAY);
    }

    public void stopLoop() {
        removeMessages(MSG_UPDATE_IMAGE);
        handlerState = State.STOP;
    }
}
